﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using perfume.Models.DB;

namespace perfume.Controllers
{
    public class CategoryController : Controller
    {
        private perfume_dbEntities db = new perfume_dbEntities();
        private Product_repository pr = new Product_repository();

        //
        // GET: /Category/

        public ViewResult Index()
        {
            var products = db.products.Include("category");
            return View(products.ToList());
        }

        //
        // GET: /Category/Details/5

        public ViewResult Details(int id)
        {
            List<product> product = pr.GetProductByCat(id).ToList();
            return View(product);
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            ViewBag.category_id = new SelectList(db.categories, "category_id", "category_name");
            return View();
        }

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(product product)
        {
            if (ModelState.IsValid)
            {
                db.products.AddObject(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.category_id = new SelectList(db.categories, "category_id", "category_name", product.category_id);
            return View(product);
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(int id)
        {
            product product = db.products.Single(p => p.product_id == id);
            ViewBag.category_id = new SelectList(db.categories, "category_id", "category_name", product.category_id);
            return View(product);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(product product)
        {
            if (ModelState.IsValid)
            {
                db.products.Attach(product);
                db.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.category_id = new SelectList(db.categories, "category_id", "category_name", product.category_id);
            return View(product);
        }

        //
        // GET: /Category/Delete/5

        public ActionResult Delete(int id)
        {
            product product = db.products.Single(p => p.product_id == id);
            return View(product);
        }

        //
        // POST: /Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            product product = db.products.Single(p => p.product_id == id);
            db.products.DeleteObject(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}